#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN DATA ABSTRACTION LAYER version 1

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
using System.Collections;
using Thn.Application;
#endregion

namespace Thn.Data
{
    /// <summary>
    /// Hold default values
    /// </summary>
    public class DefaultValues
    {
        /// <summary>
        /// Contains default values
        /// <para>Key   : Type</para>
        /// <para>Values: default value of that type</para>
        /// </summary>
        Hashtable defaultHash = null;

        #region Get
        /// <summary>
        /// Gets the default value of a certain data type
        /// </summary>
        public static object Get(Type dataType)
        {
            return Instance.GetDefault(dataType);
        }

        object GetDefault(Type dataType)
        {
            object result = null;

            if (defaultHash.Contains(dataType)) result = defaultHash[dataType];
            
            return result;
        }
        #endregion

        #region Instance
        /// <summary>
		/// Static variable for thread locking
		/// </summary>
		static object SyncRoot = new object();
		
		/// <summary>
		/// Actual static variable for Instance
		/// </summary>
		static DefaultValues mInstance = null;
		
		/// <summary>
		/// Gets the static instance of this class
		/// </summary>
		static DefaultValues Instance
		{
			get
			{
				if (mInstance == null)
				{
					//initialize new instance
					DefaultValues tmp = new DefaultValues();
					
					//assign to static variable. Lock root for thread-safe locking
					lock (SyncRoot)
					{
						mInstance = tmp;
					}
				}

				return mInstance;
			}
		}
		
		#endregion
		
        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        DefaultValues()
        {
            defaultHash = new Hashtable();
            defaultHash.Add(typeof(string), "");
            defaultHash.Add(typeof(DateTime), Nulls.DateTime);
            defaultHash.Add(typeof(Guid), Nulls.Guid);
            defaultHash.Add(typeof(int), 0);
            defaultHash.Add(typeof(double), 0d);
            defaultHash.Add(typeof(decimal), 0M);
            defaultHash.Add(typeof(float), 0f);            
        }
        #endregion
    }
}
